<!--
	作者：offline
	时间：20181208
	描述：已确认采购订单列表(lxh)
-->
<template>
	<div class="m-view-confirmedPurOrder m-window">
		<div class="m-condition" v-if="diaShow">
			<el-row :gutter="5" >
				<el-col :span="6">
					<el-select value-key="id" v-model="selCompanyCode" placeholder="请选择客户" @change="companyChange" >
						<el-option v-for="item in comOptions" :key="item.id" :label="item.companyName" :value="item"></el-option>
					</el-select>
				</el-col>
				<el-col :span="6">
					<el-input v-model="purOrdNo" placeholder="请输入采购订单号" clearable ></el-input>
				</el-col>
				<el-col :span="4">
					<el-date-picker v-model="beginDate"  type="date" placeholder="开始日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd"></el-date-picker>
				</el-col>
				<el-col :span="4">
					<el-date-picker v-model="endDate"  type="date" placeholder="结束日期" :picker-options="pickerEndOptions" value-format="yyyy-MM-dd"></el-date-picker>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="search" :disabled="btnDis">查询</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom" v-if="diaShow">
			<agGridComponents ref="agGridComponent" :getGridOptions="gridOptions" :getColumnDefs="columnDefs"></agGridComponents>
		</div>
		<div class="m-page" v-if="diaShow">
        	<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" @prev-click="handlePrevClick" @next-click="handleNextClick" :current-page="currentPage" :page-sizes="[30,50,100,150,200]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
		</div>

		<div class="m-window" v-if="componentsShow">
	    	<purOrderDetailsComponents @cancelComponents="cancelComponents" :getClickValue="getClickValue" :ordStatus="ordStatus"></purOrderDetailsComponents>
	    </div>
	</div>
</template>

<script>
	import _Api from "@/api";
	import localeText from "@/i18n/agGridText";
	import agGridComponents from "@/components/agGridComponents.js";
	import authInfo from "@/public/authInfo.js";
	import supplierInfo from "@/public/supplierInfo.js";
	import buttonLink from "@/components/buttonLink.js";
	import {orderType} from "@/public/enum.js";
	import purOrderDetailsComponents from "@/views/purchase/purOrderDetailsComponents.vue"

	export default{
		data(){
			return{
				selCompanyCode:"",
				comOptions:[],
				purOrdNo:"",
				beginDate:"",
				endDate:"",
				pickerOptions:{
					disabledDate: this.disabledStartDate
                },
                pickerEndOptions:{
					disabledDate: this.disabledEndDate
				},
				currentPage:1,
				pageSize:30,
				total:0,
				supplierInfo:[],
				rowData:[],
				getClickValue:"",
				diaShow:true,
				componentsShow:false,
				ordStatus:"",
				btnDis:false
			}
		},
		mounted(){
			var that = this;
			if(this.$global.user.userTypeId == "4")
        	{
//				var arrayList = [];
//				arrayList.push(this.$global.supplierData);
				this.supplierInfo.push(this.$global.supplierData);
				if(this.supplierInfo[0] == null)
				{
					supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo)=>{
	        			that.supplierInfo = supplierInfo;
	        		})
				}
        	}
        	authInfo.getAuthInfo().then((authCompanyInfo)=>{
				that.comOptions = authCompanyInfo;
			});
			this.ordStatus = 3;//已确认
		},
		beforeMount(){
			this.gridOptions = {
				context: {
					componentParent: this
				},
				rowSelection:"multiple",
				enableColResize: true,
        	    localeText: localeText,
        	    getContextMenuItems:[],
        	    defaultColDef:{
        	    	menuTabs:[]
        	    },
        	    suppressRowClickSelection: true//单元格选中,false为选中整行
			};
			this.columnDefs = [
			{	headerName:"采购订单号",field:"purOrdNo",checkboxSelection:true,cellRenderer:buttonLink},
			{	headerName:"订单类型",field:"purOrdType",cellRenderer:function(params){
					if(params.value){
						for(var i in orderType){
							if(orderType[i].value == params.value)
								return orderType[i].label;
						}
					}
				}
			},
			{	headerName:"订单日期",field:"purOrdDate",cellRenderer:function(params){
					if(params.value)
						return new Date(Date.parse(params.value.replace(/-/g,"/"))).Format("yyyy-MM-dd");
				}
			},
			{	headerName:"客户名称",field:"companyName"},
			{	headerName:"采购组",field:"purGroupName"},// purGroup
			{	headerName:"发布日期",field:"releaseDate",cellRenderer:function(params){
					if(params.value)
						return new Date(Date.parse(params.value.replace(/-/g,"/"))).Format("yyyy-MM-dd");
				}
			},
			{	headerName:"确认日期",field:"confirmDate",cellRenderer:function(params){
					if(params.value)
						return new Date(Date.parse(params.value.replace(/-/g,"/"))).Format("yyyy-MM-dd");
				}
			},
			{	headerName:"确认人",field:"confirmer"}]
		},
		components:{
			agGridComponents,
			purOrderDetailsComponents
		},
		methods:{
            //  限制开始日期选择范围
            disabledStartDate (time) {
                if (this.endDate)
                    return time.getTime() > new Date(this.endDate).getTime();
            },
            //  限制结束日期选择范围
            disabledEndDate (time) {
                if(this.beginDate)
                    return time.getTime() < (new Date(this.beginDate).getTime()- 3600 * 1000 * 24);
            },
			cancelComponents(msg){
				if(msg){
					var that = this;
					this.componentsShow = false;
					this.diaShow = true;
					this.$nextTick(_=>{
						// that.gridOptions.api.sizeColumnsToFit();
						// that.gridOptions.api.hideOverlay();
						that.search();
					});
				}
			},
			setClickValue(value){
				this.getClickValue = value.data;
				this.diaShow = false;
				this.componentsShow = true;
			},
			companyChange(){
				this.search();
			},
			handleSizeChange(params){
				this.pageSize = params;
				this.search();
			},
			handleCurrentChange(params){
				this.currentPage = params;
				this.search();
			},
			handlePrevClick(params){
				this.currentPage = params;
				this.search();
			},
			handleNextClick(params){
				this.currentPage = params;
				this.search();
			},
			search(){
				var obj ={};
				var that = this;
				obj.eqParams = {};
				obj.geParams = {};
				obj.leParams = {};
				obj.likeParams = {};
				obj.current = this.currentPage;
				obj.size = this.pageSize;
				if(this.beginDate != "" && this.beginDate != null && this.endDate != "" && this.endDate != null){
					if(this.beginDate > this.endDate)
					{
						this.$alert("提示：开始日期不能大于结束日期", {
							showClose: false,
							confirmButtonText: "确定",
							customClass: "alertPrompt"
						});
						return;
					}
				}
				obj.eqParams.supplierCode = this.supplierInfo[0].supplierCode;
				obj.eqParams.billState = 2;
				if(this.selCompanyCode)
					obj.eqParams.companyCode = this.selCompanyCode.companyCode;
				if(this.purOrdNo)
					obj.likeParams.purOrdNo = this.purOrdNo;
				if(this.beginDate != "" && this.beginDate != null)
					obj.geParams.createDate = this.beginDate;
				else if((this.beginDate == "" || this.beginDate == null) && this.endDate != "" && this.endDate != null)
					obj.geParams.createDate = this.endDate;

				if(this.endDate != "" && this.endDate != null)
					obj.leParams.createDate = this.endDate;
				else if((this.endDate == "" || this.endDate == null) && this.beginDate != "" && this.beginDate != null)
					obj.leParams.createdate = this.beginDate;

				this.$refs.agGridComponent.loading = true;
				
				_Api.getConfirmedPurOrder(obj).then(function(response){
					var data =response.data;
					if(data.code == "1"){
						if(data.total > 0){
							that.rowData = data.rows.splice(0);
							that.$refs.agGridComponent.gridOptions.api.setRowData(that.rowData);
							that.total = data.total;
						}else{
		                    that.$refs.agGridComponent.gridOptions.api.setRowData([]);
							that.total = 0;
						}
						that.$refs.agGridComponent.loading = false;
					}else{
						that.$alert(data.info, {
	                        showClose: false,
	                        confirmButton: "确定",
	                        customClass: "alertFailure"
	                    });
	                    that.$refs.agGridComponent.loading = false;
	                    that.$refs.agGridComponent.gridOptions.api.setRowData([]);
						that.total = 0;
					}
				}).catch((err)=>{
					that.$alert(err.data.message, {
                        showClose: false,
                        confirmButton: "确定",
                        customClass: "alertFailure"
                    });
                    that.$refs.agGridComponent.loading = false;
                    that.$refs.agGridComponent.gridOptions.api.setRowData([]);
					that.total = 0;
				})
			}
		}
	}
</script>